package com.mobile.web.pensionDeclaration;

import com.mobile.service.dto.PensionDeclarationDTO;
import com.mobile.service.dto.PensionDeclarationQueryDTO;
import com.mobile.service.inter.PensionDeclarationService;
import com.mobile.util.*;
import com.mobile.web.base.BaseController;
import com.mobile.util.config.aop.AutoFill;
import com.mobile.util.config.aop.OperateType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 养老保险申请表API接口
 */
@RestController
@RequestMapping("/pensionDeclaration")
@Tag(name = "养老保险申请管理接口", description = "包含养老保险申请的查询、新增、修改、删除等操作")
public class PensionDeclarationController extends BaseController {

    @Autowired
    private PensionDeclarationService pensionDeclarationService;

    @PostMapping("/addPensionDeclaration")
    //@RequiresPermissions("pensionDeclarationmana:add:1")
    @Operation(summary = "创建养老保险申请")
    @AutoFill(OperateType.INSERT)
    public Object addPensionDeclaration(@Valid @RequestBody PensionDeclarationDTO dto) {
        Map<String, Object> map;
        try {
            map = pensionDeclarationService.addPensionDeclaration(dto);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping(value = "/getPensionDeclarationListPage")
    //@RequiresPermissions("pensionDeclarationmana:read:1")
    @Operation(summary = "分页查询养老保险申请")
    public Object getPensionDeclarationListPage(@Validated PensionDeclarationQueryDTO queryDTO) {
        Map<String, Object> map = new HashMap<>();
        try {
            PageResult<PensionDeclarationDTO> page = pensionDeclarationService.getPensionDeclarationListPage(queryDTO);
            map.put("page", page);
            map.put("data", page.getPageDatas());
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping(value = "/getPensionDeclarationById")
    //@RequiresPermissions("pensionDeclarationmana:edit:1")
    @Operation(summary = "查询养老保险申请详情")
    public Object getPensionDeclarationById(
        @Parameter(description = "养老保险申请主键", required = true) 
        @RequestParam("id") String id) {
        Map<String, Object> map = new HashMap<>();
        try {
            PensionDeclarationDTO dto = pensionDeclarationService.getPensionDeclarationById(id);
            if (StringUtil.isEmpty(dto)) {
                map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
                map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
            } else {
                map.put("data", dto);
                map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
            }
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @PostMapping("/updatePensionDeclaration")
    //@RequiresPermissions("PensionDeclarationmana:edit:1")
    @Operation(summary = "编辑养老保险申请")
    @AutoFill(OperateType.UPDATE)
    public Object updatePensionDeclaration(@Valid @RequestBody PensionDeclarationDTO dto) {
        Map<String, Object> map;
        try {
            map = pensionDeclarationService.updatePensionDeclaration(dto);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping("/removePensionDeclaration")
    //@RequiresPermissions("pensionDeclarationmana:delete:1")
    public Object removePensionDeclaration(@RequestParam("id") String id) {
        Map<String, Object> map;
        try {
            map = pensionDeclarationService.removePensionDeclaration(id);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping("/getPensionDeclarationList")
    //@RequiresPermissions("pensionDeclarationmana:read:1")
    @Operation(summary = "条件查询养老保险申请列表")
    public Object getPensionDeclarationList(@Validated PensionDeclarationQueryDTO queryDTO) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<PensionDeclarationDTO> list = pensionDeclarationService.getPensionDeclarationList(queryDTO);
            map.put("data", list);
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }
}
